<?php
namespace Admin\Controller;

class CountLikeController extends \Common\Controller\CountLikeController {

    public function __construct()
    {
        parent::__construct();
        $uid = session('userid');
        if (!$uid){
            $this->error('未登录','/Admin/Base/login',3);
        }
    }

    public function index(){
        self::user_list();
    }

    //用户模块
    public function user_list()
    {
        $where = array();
        $where['countlike_member_id'] = I('get.uid','','int');
        $where['prize_num'] = I('get.prize_num','','int');

        $nickname = I('get.nickname','','htmlspecialchars');

        if ($nickname){
            $where['nickname'] = array('like',"%$nickname%");
        }


        $where                              =   array_filter($where);

        $User       =   M('countlike_member'); // 实例化User对象
        $sum = array();

        if (!empty($where)){
            $count = $User->where($where)->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count, 50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show = $Page->show();// 分页显示输出

            $field      =   array('countlike_member_id','nickname','avatarurl','province','city','game_num','prize_num','totle_num','money_total','total_money','lock_money','login_num','createtime','logintime');
            $data['user_list'] = $User->field($field)->where($where)->order('countlike_member_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

            foreach ($data['user_list'] as $key=>$val){

                $data['user_list'][$key]['frendnum'] = M('countlike_relationship')->where(array('my_member_id'=>$val['countlike_member_id']))->count();
                $data['user_list'][$key]['sharegroup']  = M('countlike_wechat_group')->where(array('countlike_member_id'=>$val['countlike_member_id']))->count();

                $sum['frendnum'] += $data['user_list'][$key]['frendnum'];
                $sum['sharegroup'] += $data['user_list'][$key]['sharegroup'];
                $sum['game_num'] +=$val['game_num'];
                $sum['prize_num'] +=$val['prize_num'];
                $sum['totle_num'] +=$val['totle_num'];

                $sum['money_total'] +=$val['money_total'];

                $sum['total_money'] +=$val['total_money'];

                $sum['lock_money'] +=$val['lock_money'];
            }
            $this->assign('page', $show);// 赋值分页输出
            $this->assign("data",$data);
            $this->assign("sum",$sum);
            $this->display('user_list');
        }else {
            $count = $User->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count, 50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show = $Page->show();// 分页显示输出

            $field = array('countlike_member_id', 'nickname', 'avatarurl', 'province', 'city', 'game_num', 'prize_num', 'totle_num', 'money_total', 'total_money', 'lock_money', 'login_num', 'createtime', 'logintime');
            $data['user_list'] = $User->field($field)->order('countlike_member_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

            foreach ($data['user_list'] as $key => $val) {

                $data['user_list'][$key]['frendnum'] = M('countlike_relationship')->where(array('my_member_id' => $val['countlike_member_id']))->count();
                $data['user_list'][$key]['sharegroup'] = M('countlike_wechat_group')->where(array('countlike_member_id' => $val['countlike_member_id']))->count();
                $sum['frendnum'] += $data['user_list'][$key]['frendnum'];
                $sum['sharegroup'] += $data['user_list'][$key]['sharegroup'];
                $sum['game_num'] +=$val['game_num'];
                $sum['prize_num'] +=$val['prize_num'];
                $sum['totle_num'] +=$val['totle_num'];

                $sum['money_total'] +=$val['money_total'];

                $sum['total_money'] +=$val['total_money'];

                $sum['lock_money'] +=$val['lock_money'];
            }
            $this->assign("sum",$sum);
            $this->assign('page', $show);// 赋值分页输出
            $this->assign("data", $data);
            $this->display('user_list');
        }
    }

    public function user_info()
    {

    }

    public function user_edit()
    {

    }

    //游戏规则
    public function game_rule()
    {
        $data['rule']   =   M('countlike_rule')->find();
        $this->assign("data",$data);
        $this->display('game_rule');
    }
    //清缓存
    public function clear_cache(){

        $this->display('clear_cache');
    }

    public function game_rule_edit()
    {
        $where['countlike_money_id'] =   I('post.countlike_money_id','','htmlspecialchars');
        $data['sharegroup']     =   I('post.sharegroup','','htmlspecialchars');
        $data['answer']         =   I('post.answer','','htmlspecialchars');
        $data['wrong']          =   I('post.wrong','','htmlspecialchars');
        $data['sharefrend']     =   I('post.sharefrend','','htmlspecialchars');
        $data['resurgence']     =   I('post.resurgence','','htmlspecialchars');

        $data['appid']          =   I('post.appid','','htmlspecialchars');

        //安全判断

        $result     =   M('countlike_rule')->where($where)->save($data); // 根据条件更新记录

        //记录相关操作日志

        if (!empty($result)) 
        {
            $msg['state']   =   200;
            $msg['error']   =   '更新成功';
            $this->ajaxReturn ($msg,'JSON');
        }
        else
        {
            $msg['state']   =   -1;
            $msg['error']   =   '更新失败';
            $this->ajaxReturn ($msg,'JSON');
        }
    }

    public function game_rank(){

        $data = M('countlike_rank')->select();

        $this->assign('rank',$data);
        $this->display('game_rank');
    }

    //礼物领取记录(红包)
    public function game_redbag()
    {

        $where = array();
        $where['member_id'] = I('get.uid','','int');
        $where = array_filter($where);

        $Module         =   M('countlike_redbag'); // 实例化对象

        if (empty($where)){
            $count          =   $Module->count();// 查询满足要求的总记录数
        }else {
            $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
        }

        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $field          =   array('challenge_id','red_total','create_time','update_time','status','total_sn','member_id');
        $order_by['challenge_id']   =   'desc';

        if (empty($where)){
            $data['lyricist_redbag']     =   $Module->field($field)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();
        }else{
            $data['lyricist_redbag']     =   $Module->field($field)->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();
        }

        $sum = 0;
        foreach ($data['lyricist_redbag'] as $key=>$val){

            $sum += $val['red_total'];
        }

        $sum = sprintf("%.2f", $sum);

        $this->assign('page',$show);// 赋值分页输出

        $this->assign('sum',$sum);// 列总钱数

        $this->assign("data",$data);

        $this->display('game_redbag');
    }

    //审核中
    public function game_cash()
    {

        if (IS_AJAX){

            $where['id'] = I('post.id','','htmlspecialchars');

            if (empty($where['id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $user = M('countlike_cash')->field('uid,money')->where($where)->find();

            $uid = $user['uid'];

            $upchsh = M('countlike_cash')->where($where)->save(array('cash_status'=>'1'));
            M('countlike_member')->where(array('countlike_member_id'=>$uid))->save(array('lock_money'=>'0'));
            M('countlike_member')->where(array('countlike_member_id'=>$uid))->setInc('used_money',$user['money']);



            if (!empty($upchsh))
            {
                $msg['state']   =   200;
                $msg['error']   =   '成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '失败';
                $this->ajaxReturn ($msg,'JSON');
            }


        }else{

            $Module         =   M('countlike_cash'); // 实例化对象
            $where['cash_status'] = 0;
            $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
            $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show           =   $Page->show();// 分页显示输出

            $order_by['id']   =   'asc';
            $data     =   $Module->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();


            $this->assign('page',$show);// 赋值分页输出

            $this->assign("data",$data);
            $this->display('game_cash');
        }
    }

//审核过
    public function game_cash1()
    {

        $Module         =   M('countlike_cash'); // 实例化对象
        $where['cash_status'] = 1;
        $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $order_by['id']   =   'asc';
        $data     =   $Module->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();


        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_cash1');
    }

    //所有
    public function game_cash2()
    {

        $Module         =   M('countlike_cash'); // 实例化对象

        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $order_by['id']   =   'asc';
        $data     =   $Module->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();


        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_cash2');
    }



    public function game_rank_add()
    {
        if (IS_AJAX)
        {
            $data['rank']     =   I('post.rank','','htmlspecialchars');
            $data['rankname']       =   I('post.rankname','','htmlspecialchars');
            $data['time']=   I('post.time','','htmlspecialchars');
            $data['nums']      =   I('post.nums','','htmlspecialchars');
            $data['money']   =   I('post.money','','htmlspecialchars');


            $result     =   M('countlike_rank')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_rank_add');
        }
    }

    public function game_rank_edit()
    {
        if (IS_AJAX)
        {
            $where['countlike_rank_id']  =   I('post.countlike_rank_id','','htmlspecialchars');
            if (empty($where['countlike_rank_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $data['rank']     =   I('post.rank','','htmlspecialchars');
            $data['rankname']       =   I('post.rankname','','htmlspecialchars');
            $data['time']=   I('post.time','','htmlspecialchars');
            $data['nums']      =   I('post.nums','','htmlspecialchars');
            $data['money']   =   I('post.money','','htmlspecialchars');

            $result     =   M('countlike_rank')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result))
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['countlike_rank_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['countlike_rank_id']))
            {
                $this->error('非法请求');
            }

            $data['game_rank']  =   M('countlike_rank')->where($where)->find();

            if ( empty($data))
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_rank_edit');
        }
    }

    /**
     * 删除一条等级记录
     */
    public function game_rank_del(){
        if (IS_AJAX) {
            $where['countlike_rank_id'] = I('post.id', '', 'int');

            if (empty($where['countlike_rank_id'])) {
                $msg['state'] = -1;
                $msg['error'] = '非法请求';
                $this->ajaxReturn($msg, 'JSON');
            }
            $result = M('countlike_rank')->where($where)->delete(); // 根据条件删除记录

            //记录相关操作日志

            if (!empty($result)) {
                $msg['state'] = 200;
                $msg['error'] = '删除成功';
                $this->ajaxReturn($msg, 'JSON');
            } else {
                $msg['state'] = -1;
                $msg['error'] = '删除失败';
                $this->ajaxReturn($msg, 'JSON');
            }

        }

    }

    //更新缓存
    public function game_updatecache(){

        $category = I('post.category','','htmlspecialchars');

        switch ($category)
        {
            case 'allrank':
                S('allrank',null);
              $cache = $this->getcacherank();
                break;

            case 'questions':
                S('countlike_questions',null);
                $cache = $this->get_question(1,10);
                break;

            case 'rank':
                S('countlike_rank',null);
                $cache = $this->get_rank(1);
                break;

            case 'rule':
                S('countlike_rule',null);
               $cache = $this->get_rule();
                break;

            case 'signconfig':
                S('countlike_signin_config',null);
                $cache = $this->get_sign_config();
                break;
                default;
        }

        if (!empty($cache)) {
            $msg['state'] = 200;
            $msg['error'] = '更新成功';
            $this->ajaxReturn($msg, 'JSON');
        } else {
            $msg['state'] = -1;
            $msg['error'] = '更新失败';
            $this->ajaxReturn($msg, 'JSON');
        }


    }

    //更多好玩
    public function game_more()
    {
        $data['game_list']  =   M('countlike_appshow')->select();
        $this->assign("data",$data);
        $this->display('game_more');
    }

    public function game_more_add()
    {
        if (IS_AJAX) 
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('countlike_appshow')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_more_add');
        }
    }



    public function game_more_edit()
    {
        if (IS_AJAX) 
        {
            $where['countlike_appshow_id']  =   I('post.countlike_appshow_id','','htmlspecialchars');
            if (empty($where['countlike_appshow_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');    
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('countlike_appshow')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['countlike_appshow_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['countlike_appshow_id']))
            {
                $this->error('非法请求');
            }

            $data['game_info']  =   M('countlike_appshow')->where($where)->find();

            if ( empty($data)) 
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_more_edit');
        }
    }

    //游戏题库
    public function game_question()
    {
        $data['game_question']  =   M('countlike_questions')->order('countlike_questions_id desc')->select();
        $this->assign("data",$data);
        $this->display('game_question');
    }

    public function game_question_add()
    {
        exit;

    }

    public function game_question_edit()
    {

    }

    //分享微信群
    public function wechat_group()
    {

        $data['wechat_group']  =   M()->table('mj_countlike_wechat_group w')->join('mj_countlike_member as uesr on  w.countlike_member_id = uesr.countlike_member_id')->field('w.*,uesr.nickname,uesr.avatarurl')->order('w.id desc')->select();

        $this->assign("data",$data);
        $this->display('wechat_group');
    }

    //世界排行
    public function all_ranking()
    {
        $rank = M('countlike_member')->field('countlike_member_id,nickname,avatarurl,money,rankname')->order('money desc')->limit(100)->select();

        foreach ($rank as $key=>$value){

            $rank[$key]['money'] = $rank[$key]['money']/10000;
            $rank[$key]['money'] = $rank[$key]['money'].'万金币';
        }


        $this->assign("data",$rank);
        $this->display('all_ranking');
    }

    public function frend_ranking(){

        $member_id = I('get.member_id','','int');

        $where['my_member_id'] = $member_id;


        $coupling_uids = M('countlike_relationship')->where($where)->getField('coupling_member_id',true);
        if (empty($coupling_uids)){
            $coupling_uids = array();
        }
        $coupling_uids[] = $member_id;


        $uids = M('countlike_relationship')->where(array('coupling_member_id'=>$member_id))->getField('my_member_id',true);
        if (empty($uids)){
            $uids = array();
        }

        $ids = array_unique(array_merge($coupling_uids,$uids));//合并去重

        $where1['countlike_member_id'] = array('in',$ids);
        $rank = "";
        if ($ids){
            $rank = M('countlike_member')->field('countlike_member_id,nickname,avatarurl,money,rankname')->where($where1)->order('money desc')->select();

            foreach ($rank as $key=>$value){
                $rank[$key]['money'] = $rank[$key]['money']/10000;
                $rank[$key]['money'] = $rank[$key]['money'].'万金币';
            }

        }

        $this->assign('data',$rank);
        $this->display('frend_ranking');
    }


}